<?php
/**
 * Data model
 *
 * @category  DataModel
 * @package   DataModel
 * @author    DataModel Generator v1.1
 * @version   Revision: 127 
 */

/**
 * Data model for table "phpdoc_class"
 *
 * @category  DataModel
 * @package   DataModel
 * @author    DataModel Generator v1.1
 * @version   Revision: 127 
 */
class PhpdocClass extends BasePhpdocClass
{
    public function findByWords(array $words)
    {
        $q = ORMQuery::selectBuilder()
            ->from('Phpclass pc');
        foreach($words as $word) {
            $word = trim($word);
            $q ->orWhere('pc.name LIKE ?','%' . $word . '%');
            $q ->orWhere('pc.extends LIKE ?','%' . $word . '%');
            $q ->orWhere('pc.implements LIKE ?','%' . $word . '%');
        }
        return $q->fetchAll('Phpclass');
    }
    
    public function getTags()
    {
        $res = array();
        foreach($this->PhpdocTag as $tag) {
            if($tag->name == 'author' || $tag->name == 'todo') {
                if(!array_key_exists($tag->name,$res)) {
                    $res[$tag->name] = array();
                }
                $res[$tag->name][] = $tag->value;            
            } else {
                $res[$tag->name] = $tag->value;
            }
        }
        //print_r($res); exit;
        return $res;
    }

    public function getClassTree()
    {
        $res = array();
        $q = ORMQuery::selectBuilder();
        $q  ->from( 'PhpdocClassRefClass ref')
            ->andWhere( 'ref.class_id = ?', $this->id )
            ->orderBy('ref.distance DESC');
        $refs = $q->fetchAll('PhpdocClassRefClass');
        foreach($refs as &$item) {
            $parent = $item->ParentPhpdocClass;
            if($parent->package_id != null) {
                $res[$parent->name] = $parent->name;
            } else {
                $res[$parent->name] = null;
            }
        }
        return $res;
    }

    public function getPhpDocInheritedMethods()
    {
        $q = ORMQuery::selectBuilder();
        $q  ->from( 'PhpDocInheritedMethods ref')
            ->andWhere( 'ref.class_id = ?', $this->id )
            ->orderBy('ref.distance');
        return $q->fetchAll('PhpDocInheritedMethods');
    }

    public function getPhpDocInheritedVariables()
    {
        $q = ORMQuery::selectBuilder();
        $q  ->from( 'PhpDocInheritedVariables ref')
            ->andWhere( 'ref.class_id = ?', $this->id )
            ->orderBy('ref.distance');
        return $q->fetchAll('PhpDocInheritedVariables');
    }

    public function getPhpDocInheritedConsts()
    {
        $q = ORMQuery::selectBuilder();
        $q  ->from( 'PhpDocInheritedConstants ref')
            ->andWhere( 'ref.class_id = ?', $this->id )
            ->orderBy('ref.distance');
        return $q->fetchAll('PhpDocInheritedConstants');
    }
}
